using DevExpress.Xpf.Editors;
using DevExpress.Xpo;
using DXApplications.Models;
using DXApplications.ViewModels;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DXApplications.Views
{
    /// <summary>
    /// Interaction logic for View1.xaml
    /// </summary>
    public partial class MainView : UserControl
    {
        private MainViewModel mainViewModel;
        public MainView()
        {
            InitializeComponent();
            mainViewModel=this.DataContext as MainViewModel;
        }

        private async void ButtonInfo_Click(object sender, RoutedEventArgs e)

        { // 获取当前点击的 ButtonInfo
            Button buttonInfo = sender as Button;
            //获取焦点行
            TimeStampModel focusedRow = (TimeStampModel)gridControl1.SelectedItem;
            if (buttonInfo != null)
            {
                // 获取 Tag 的值
                string tag = buttonInfo.Content as string;

                // 根据 Tag 的值执行不同的操作
                switch (tag)
                {
                    case "打开":
                        {
                            if (focusedRow.OpenTime == DateTime.MinValue)
                            {
                                focusedRow.OpenTime = DateTime.Now;
                                
                            }
                          
                             mainViewModel.OpenFile(focusedRow.FilePath);
                            break;
                        }
                        case "删除":
                        {
                            MessageBoxResult result = MessageBox.Show("确定要删除该文件吗？", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                            if (result == MessageBoxResult.No)
                                return;
                            TimeStampModel timeStampModel = focusedRow as TimeStampModel;
                            await mainViewModel.DeleteFileAsync(timeStampModel);
                            await mainViewModel.UpdateViewAsync();
                            break;
                        }
                    case "记住了":
                       
                        if (focusedRow.OpenTime == DateTime.MinValue)
                        {
                            focusedRow.Time = focusedRow.OpenTime = DateTime.Now;

                        }
                        else
                        {
                             focusedRow.Time = DateTime.Now;
                        }
                            await mainViewModel.AddTimeStampAsync(focusedRow);
                        break;
                    default:
                        // 处理未知的 Tag
                        MessageBox.Show("未知操作");
                        break;
                }
            }

        }

        private async void brows_Click(object sender, RoutedEventArgs e)
        {
            // 使用FindName方法获取控件引用
            var fileTypeComboBox = FindName("fileTypeComboBox") as DevExpress.Xpf.Editors.ComboBoxEdit;
            if(fileTypeComboBox == null || fileTypeComboBox.SelectedItem == null)
            {
                MessageBox.Show("请先选择文件类型");
                return;
            }
            
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "所有文件 (*.*)|*.*";
            openFileDialog.Title = "选择文件";
            openFileDialog.Multiselect = true;

            if (openFileDialog.ShowDialog() == true)
            {
                 string[] names = openFileDialog.FileNames;
                foreach (string name in names)
                {
                    TimeStampModel fileInfo = new TimeStampModel
                    {
                        FileName = System.IO.Path.GetFileName(name),
                        FilePath = name,
                        Time = DateTime.Now,
                        OpenTime=DateTime.Now,
                        TypeName = (fileTypeComboBox.SelectedItem as TypesModel).FileTypeName,
                        TypeID= (fileTypeComboBox.SelectedItem as TypesModel).FileTypeID
                    };
                    await mainViewModel.AddFileAsync(fileInfo);
                }
            }

        }

        private bool isInitialLoad = true;
        private async void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if( e.NewValue is bool isVisible && isVisible)
            {
                // 仅在非首次加载时更新视图，避免重复加载数据
                if (!isInitialLoad)
                {
                    await mainViewModel.UpdateTypesAsync();
                    await mainViewModel.UpdateViewAsync();
                }
                // 标记为已完成首次加载
                isInitialLoad = false;
            }
        }
    }
}
